﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Transactions;
using Core.Contract.DataContract;
using Core.DAO;
using Data.BO;
using www.Models;

namespace Controllerss
{
   public class ContactController
   {
       #region value
       private readonly ContactBO _contactBo=new ContactBO();
       #endregion

       #region ham lay du lieu

       public IQueryable<RelaxContactDataContract> GetContactDataContracts()
       {
           using (var scope = new TransactionScope())
           {
               var lReturn = _contactBo.GetAllContactDataContracts();
               scope.Complete();
               return lReturn;
           }
       }
       #endregion

       #region Add

       public long Add(RelaxContactDataContract contactDataContract)
       {
           using (var scope = new TransactionScope())
           {
               var iD = 0;
               var contact = new CONTACT
               {
                   name = contactDataContract.Name,
                   company = contactDataContract.Company,
                   address = contactDataContract.Address,
                   tel = contactDataContract.Tel,
                   mail = contactDataContract.Email,
                   title = contactDataContract.Title,
                   detail = contactDataContract.Detail,
                   decription = contactDataContract.Decription,
                   date = contactDataContract.Date,
                   isanswer = contactDataContract.IsAnswer,
                   type = contactDataContract.Type,
                   active = contactDataContract.Active
               };

               _contactBo.Add(contact);

               var first = _contactBo.GetAllContactDataContracts().OrderByDescending(o => o.Id).FirstOrDefault();
               if (first != null)
                   iD = Convert.ToInt32(first.Id);

               scope.Complete();
               return iD;
           }
       }
       #endregion

       #region Update
       public Boolean Update(RelaxContactDataContract contactDataContract)
       {
           using (var scope = new TransactionScope())
           {
              
               var contact = new CONTACT
               {
                   id = Convert.ToInt32(contactDataContract.Id),
                   name = contactDataContract.Name,
                   company = contactDataContract.Company,
                   address = contactDataContract.Address,
                   tel = contactDataContract.Tel,
                   mail = contactDataContract.Email,
                   title = contactDataContract.Title,
                   detail = contactDataContract.Detail,
                   decription = contactDataContract.Decription,
                   date = contactDataContract.Date,
                   isanswer = contactDataContract.IsAnswer,
                   type = contactDataContract.Type,
                   active = contactDataContract.Active
               };

               _contactBo.Update(contact);

               scope.Complete();
               return true;
           }
       }
       #endregion

       #region Delete

       public Boolean Delete(List<long> lId)
       {
           using (var scope = new TransactionScope())
           {
               foreach (var id in lId)
               {
                   _contactBo.Delete(id);
               }
               scope.Complete();
               return true;
           }
       }
       #endregion

   }
}
